﻿/// <reference path="QUnit/qunit-1.11.0.js" />
/// <reference path="../script/catalog.js" />

module('Storage tests');
asyncTest("Storage product get test", function () {

    Storage.getAllProducts(function () {
        ok(true, "No erors connectiong to the DB.");
        start();
    });
});

module('Product tests');
test("Product construct test", function () {
    var newProduct = new ProductVM('Test', 123);

    strictEqual(ko.toJS(newProduct.productName), 'Test', "Names match.");
    strictEqual(ko.toJS(newProduct.price), 123, "Prices match.");
});

test("Corect, content adding", function () {
    var newProduct = new ProductVM('Test', 123);
    var expected = 123;

    strictEqual(JSON.stringify(newProduct.price()), "123");

})

module('Site components tests');
test("Add form products initial length test", function () {
    var newAddForm = SiteComponents.getAddForm();
    var exptected = 1;

    strictEqual(newAddForm.products().length, exptected);
});

test("Add form products after add length test", function () {
    var newAddForm = SiteComponents.getAddForm();
    newAddForm.addProduct();

    var exptected = 2;

    strictEqual(newAddForm.products().length, exptected);
});

test("Add form products after 100 add length test", function () {
    var newAddForm = SiteComponents.getAddForm();
    for (var i = 0; i < 100; i++) {
        newAddForm.addProduct();
    }

    var exptected = 101;

    strictEqual(newAddForm.products().length, exptected);
});

test("Search form initial length test", function () {
    var newSearchForm = SiteComponents.getSearchForm();

    var exptected = 1;

    strictEqual(newSearchForm.products().length, exptected);
});

test("Search form initial content test", function () {
    var newSearchForm = SiteComponents.getSearchForm();

    var expected = JSON.stringify({});

    strictEqual(JSON.stringify(newSearchForm.products()[0]), expected);
});

test("GridView initial button state test", function () {
    var myGrid = SiteComponents.getGridViewVM();

    ok(!myGrid.sortedPriceDesc(), "Prise sort desc button active");
    ok(!myGrid.sortedPriceAsc(), "Prise sort asc button active");
    ok(!myGrid.sortedNameDesc(), "Name sort desc button active");
    ok(!myGrid.sortedNameAsc(), "Name sort asc button active");
});

test("GridView initial array state test", function () {
    var myGrid = SiteComponents.getGridViewVM();

    ok(myGrid.products().length == 0, "Initial array is empty.");
});

test("GridView array add test", function () {
    var myGrid = SiteComponents.getGridViewVM();

    for (var i = 0; i < 50; i++) {
        myGrid.addProduct(new ProductVM("Tester!", 123));
    }

    ok(myGrid.products().length == 50, "Adding is working.");
});

test("GridView starting page index test", function () {
    var myGrid = SiteComponents.getGridViewVM();

    equal(myGrid.gridViewModel.currentPageIndex(), 0, "Starting page is correct.");
});

test("GridView jump to firs page index test", function () {
    var myGrid = SiteComponents.getGridViewVM();

    myGrid.gridViewModel.currentPageIndex(3);
    myGrid.jumpToFirstPage();

    equal(myGrid.gridViewModel.currentPageIndex(), 0, "First page jumping works.");
});

test("TopMenu initial state test", function () {
    var myGrid = SiteComponents.getTopMenu();

    var exptected = ['List Catalog', 'Add Product', 'Search for product'];

    deepEqual(myGrid.menus, exptected, "Start menus are correct");
});

module('Cash register test');
test("Cash register init test", function () {
    var myReg = new CashRegister(5000);
    var expected = 5000;

    strictEqual(myReg.balance, expected, "Balance is correctly created");
});

test("Cash register get USD test", function () {
    var myReg = new CashRegister(5000);
    var expected = 7500;

    strictEqual(myReg.getCashInUSD(), expected, "Balance is correctly created");
});

test("Cash register get Euro test", function () {
    var myReg = new CashRegister(5000);
    var expected = 9900;

    strictEqual(myReg.getCashInEuro(), expected, "Balance is correctly created");
});

test("Cash register get Euro test", function () {
    var myReg = new CashRegister(5000);
    var expected = 9900;

    strictEqual(myReg.getCashInEuro(), expected, "Balance is correctly created");
});

test("Cash register add test", function () {
    var myReg = new CashRegister(5000);
    var expected = 5500;
    myReg.add(500);

    strictEqual(myReg.balance, expected, "Balance is correctly created");
});

test("Cash register remove test", function () {
    var myReg = new CashRegister(5000);
    var expected = 4500;
    myReg.remove(500);

    strictEqual(myReg.balance, expected, "Balance is correctly created");
});

